package com.aos.controller;

import com.aos.service.user.UserService;
import com.aos.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName AuthenticationController
 * @Description:鉴权控制器
 * @Author wumingwang
 * @Date 2018/9/13 10:43
 * @Version 1.0
 */
@Slf4j
@RestController
@Api(value = "Authentication Controller", tags = {"鉴权接口,服务内部调用"})
public class AuthenticationController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "用户鉴权", notes = "用户鉴权")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token",  required = true, dataType = "String"),
            @ApiImplicitParam(name = "permission", value = "权限URL",  required = true, dataType = "String"),
            @ApiImplicitParam(name = "appCode", value = "应用code",  required = true, dataType = "String"),
    })
    @PostMapping("/user/authentication")
    public Object authentication(String permission,String appCode,HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if(StringUtils.isEmpty(token)){
            return Result.error("token不能为空！");
        }
        if(StringUtils.isEmpty(permission)){
            return Result.error("权限URL不能为空！");
        }
        if(StringUtils.isEmpty(appCode)){
            return Result.error("应用code不能为空！");
        }
        return userService.authentication(token,permission,appCode);
    }

    @ApiOperation(value = "获取当前登录用户", notes = "获取当前登录用户，内部服务调用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token",  required = true, dataType = "String"),
    })
    @PostMapping("/user/getCurrentUser")
    public Object getCurrentUser(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if(StringUtils.isEmpty(token)){
            return Result.error("token不能为空！");
        }
        return userService.getCurrentUser(token);
    }
}
